# For Gandalf 
JAVACUP := /usr/share/lib/java-cup-11a.jar
JAVACUP_EXE := $(JAVACUP)
JAVA_HOME := /usr/local/java/jdk1.6.0_07
JAVAC := $(JAVA_HOME)/bin/javac
JDBC := /opt/jdbc/postgresql-9.0-801.jdbc4.jar

#JDBC := /usr/share/jdbc-postgresql/lib/jdbc-postgresql.jar
#JAVACUP := /usr/share/javacup/lib/javacup-runtime.jar
#JAVACUP_EXE := /usr/share/javacup/lib/javacup.jar

SOURCE_DIR := src
OUTPUT_DIR := bin
CUP_NAME := PSST
PACKAGE := cos480/amiwsd

# redefine RM to remove directories
RM := rm -r

CLASSPATH := $(OUTPUT_DIR):$(JAVACUP):$(JDBC)
JFLAGS := -cp $(CLASSPATH) -d $(OUTPUT_DIR)

#.SUFFIXES: .java .class
#.java.class:
#	$(JAVAC) $(JFLAGS) $*.java

MAIN = \
	$(SOURCE_DIR)/$(PACKAGE)/AddMgmt.java \
	$(SOURCE_DIR)/$(PACKAGE)/GenMgmt.java \
	$(SOURCE_DIR)/$(PACKAGE)/GetMgmt.java \
	$(SOURCE_DIR)/$(PACKAGE)/SearchMgmt.java \
	$(SOURCE_DIR)/$(PACKAGE)/DBMgmt.java \
	$(SOURCE_DIR)/$(PACKAGE)/PSSTulator.java \
	$(SOURCE_DIR)/$(PACKAGE)/PSSTSym.java \
	$(SOURCE_DIR)/$(PACKAGE)/PSSTLex.java \
	$(SOURCE_DIR)/$(PACKAGE)/PSSTCup.java \
	$(SOURCE_DIR)/$(PACKAGE)/AMIWSD.java

default: cup main
	echo '#! /bin/bash\nif [ $$# = 2 ]; then\n\t$(JAVA_HOME)/bin/java -Xms1g -Xmx1g BigApp -cp bin:$(JAVACUP):$(JDBC) $(PACKAGE)/AMIWSD $$1 $$2\nelse\n\t$(JAVA_HOME)/bin/java -cp bin:$(JAVACUP):$(JDBC) $(PACKAGE)/AMIWSD\nfi' > amiwsd
	chmod a+x amiwsd

#main: $(MAIN:.java=.class)
main:
	$(JAVAC) $(JFLAGS) $(SOURCE_DIR)/$(PACKAGE)/*

cup:
	jflex -d $(SOURCE_DIR)/$(PACKAGE) $(SOURCE_DIR)/$(CUP_NAME).lex
	$(JAVA_HOME)/bin/java -jar $(JAVACUP_EXE) -parser $(CUP_NAME)Cup -symbols $(CUP_NAME)Sym $(SOURCE_DIR)/$(CUP_NAME).cup
	mv $(CUP_NAME)Cup.java $(SOURCE_DIR)/$(PACKAGE)/.
	mv $(CUP_NAME)Sym.java $(SOURCE_DIR)/$(PACKAGE)/.
	
clean:
	$(RM) $(OUTPUT_DIR)/* $(SOURCE_DIR)/$(PACKAGE)/$(CUP_NAME)Cup.java $(SOURCE_DIR)/$(PACKAGE)/$(CUP_NAME)Lex.java $(SOURCE_DIR)/$(PACKAGE)/$(CUP_NAME)Sym.java amiwsd *.~ $(SOURCE_DIR)/*.~ $(SOURCE_DIR)/$(PACKAGE)/*.~ 
